<?php defined('BASEPATH') or exit('No direct script access allowed');
/**
 * Blog SAEF details (definition) file
 * 
 * @package PyroCMS
 * @subpackage Blog SAEF Module
 * @category Modules
 * @author Rick Holmes
 * @version 1.0 
 */  
class Module_Saef extends Module {

	public $version = '1.1';
	
	public function info()
	{
		return array(
			'name' => array(
				'en' => 'Blog SAEF'
			),
			'description' => array(
				'en' => 'Stand Alone Entry Form for blog entries.'
			),
			'frontend' => TRUE,
			'backend' => TRUE,
			'sections' => array(
				'items' => array(
					'name' 	=> 'saef:items',
					'uri' 	=> 'admin/saef', 
					'shortcuts' => array(
						'create_category' => array(
							'name' => 'saef:add_category',
							'uri' => 'admin/blog/categories/create',
							'class' => 'add'
						),
						'create_user_group' => array(
							'name' => 'saef:add_user_group',
							'uri' => 'admin/groups/add',
							'class' => 'add'
						)
					)
			   	)
			)
		);
	}
	
	// install creates 1 db table (allowed categories) and 3 entries in settings table	 	 	 	
	public function install()
	{
		$this->dbforge->drop_table('saef_categories');
		$this->dbforge->drop_table('saef_settings');
		$this->db->delete('settings', array('module' => 'saef'));
		
		$saef_categories = "
			CREATE TABLE `default_saef_categories` (
				`id` int(11) NOT NULL AUTO_INCREMENT,
				`cat_id` int(11) NOT NULL,
				PRIMARY KEY (`id`)
			) ENGINE=MyISAM DEFAULT CHARSET=utf8;
		";
		
		// also need to insert default data so update works later
		$saef_settings = "
			CREATE TABLE `default_saef_settings` (
				`require_moderation` TINYINT(1) NOT NULL DEFAULT 0,
				`allowed_user_groups` VARCHAR(40) NULL,
				`allow_editing` TINYINT(1) NOT NULL DEFAULT 1,
				`admin_edit` TINYINT(1) NOT NULL DEFAULT 1,
				`editable_div` VARCHAR(30) NULL
			) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
		
		$insert = "INSERT INTO `default_saef_settings` (`require_moderation`, `allowed_user_groups`, `allow_editing`, `admin_edit`, `editable_div`)
			VALUES (0, '1', 1, 1, 'single_post');";
		
		if($this->db->query($saef_categories) AND
			$this->db->query($saef_settings) AND
			$this->db->query($insert) AND
			is_dir($this->upload_path . 'saef') OR @mkdir($this->upload_path . 'saef', 0777, TRUE))
		{
			return TRUE;
		}

	}

	public function uninstall()
	{
		$this->dbforge->drop_table('saef_categories');
		$this->dbforge->drop_table('saef_settings');
		$this->db->delete('settings', array('module' => 'saef'));

		// Pyro doesn't always seem to delete the widget stuff automatically
		// CI does't seem to support joins on deletes, so...
		$this->db->query("DELETE FROM `default_widget_instances` WHERE `widget_id` = (SELECT `id` FROM `default_widgets`WHERE `slug` = 'saef');");
		$this->db->delete('widgets', array('slug' => 'saef'));
		{
			return TRUE;
		}
	}

	public function upgrade($old_version)
	{
		if ($old_version < '1.1')
		{
			$upgrade = "ALTER TABLE `default_saef_settings`
				ADD `allow_editing` TINYINT(1) NOT NULL DEFAULT 1,
				ADD `admin_edit` TINYINT(1) NOT NULL DEFAULT 1,
				ADD `editable_div` VARCHAR(30) NULL;
			";
			 
			$insert = "UPDATE `default_saef_settings` SET `allow_editing` = 1, `admin_edit` = 1, editable_div` = 'single_post';";
			
			if ($this->db->query($upgrade) AND $this->db->query($insert))
			{
				return TRUE;
			}
		}
		
		// nothing to do here (yet)
		return TRUE;
	}

	public function help()
	{
		// Return a string containing help info
		return '<h4>Overview</h4>
		<p>The SAEF module allows logged-in users to post from the front-side of a website. It is designed for use by non-admins, perhaps site members who need to post to the blog, but you do not want to confuse by having them do so in the back end.</p>
		<h4>Configuration</h4>
		<p>The SAEF admin page (Add-obs->SAEF) has three configurable settings: <p>
		<ul>			
			<li>user groups allowed to use the SAEF (default none, although administrators are always allowed)</li>
			<li>require moderation (default="no")</li>
			<li>allowed categories</li>
		</ul>
		<h5>User Groups</h5>
		<p>You may decide which user groups will be allowed to post via the SAEF. Admins are always permitted to use it. The "Add User Group" button leads to the Pyro Add User Group page for convenience.</p>
		<h5>Moderation</h5>
		<p>If "require moderation" is chosen the user will be so informed and an email will be sent to the first admin user found notifying him that the user has posted an article. Should you want a message stating that posts will be moderated before the user does anything, uncomment the relevant paragraph in ../saef/widgets/saef/views/display.php.</p>
		<h5>Categories</h5>
		<p>A listing of all current categories is shown. This allows you to limit where the user posts to, giving you control and simplifying things for the user. <strong>Note: </strong>At least one category must be checked for the SAEF to appear. The "Add Category" button leads to the Pyro Blog Categories Create page for convenience.</p>
		<h4>SAEF Widget</h4>
		<p>Simply drag the SAEF widget (Content->Widgets) to the area you wish to have the SAEF form appear.</p>
		<p>You may either style the widget in your theme\'s css file or add/change the css in the module\'s css file, which is located in the module css directory. The widget form id is "saef-form" so you should be able to override any default styling.</p>
		<p>If the SAEF widget does not appear on the front side, check to make sure the module is enabled, there are categories, the user\'s group is enabled and the user is logged in.</p>
		<h4>Inline Editing of Posts</h4>
		<p>If you have an SAEF form on the single-post ("permalink") page, the logged-in author may edit its content while on that page. (Admins may also be allowed to edit if desired.) This requires that the default classes of the post intro and body are used ("post_intro" and "post_body"). If you change these, you must modify the JavaScript source file to accommodate these changes.</p>
		<p>There is also a bit of css in saef.css to position and style these edit links. Feel free to modify this to suit your site.</p>
		<p>Should you need to have edit capability on a single-post page, but do not want to have an SAEF form, you may copy the relevant JavaScript from the source file to your own file and bind addEditLinks() on document ready or page load. More complete nstructions are in the js source file.';
	}

}
/* End of file details.php */
/* location /addons/shared_addons/modules/saef/details.php */
